#include"Ham.h"

//1.Them mot so nguyen bat ky vao dau danh sach
void insertfirst(Node* &head, int x)
{
	Node* temp = new Node;
	temp->data = x;
	temp->pNext = head;
	head=temp;
}

//2.Them mot so nguyen bat ky vao cuoi danh sach
void insertend(Node* &head,int x)
{
	if(head==NULL)
		return;
	Node* temp = new Node;
	Node* cur = head;
	while(cur->pNext)
		cur=cur->pNext;
	temp->data = x;
	cur->pNext = temp;
	temp->pNext = NULL;
}

//3.Them k vao sau x neu khong co thi chen vao cuoi
int insertkafterx(Node* &head, int k, int x)
{
	Node* cur=head;
	if(head==NULL)
	{
		head = new Node;
		head->data=k;
		head->pNext=NULL;
		return 0;
	}
	while (cur->pNext!=NULL && cur->data!=x)
		cur=cur->pNext;
	Node* temp=cur->pNext;
	cur->pNext = new Node;
	cur=cur->pNext;
	cur->data=k;
	cur->pNext=temp;
	return 0;
}

// 1: Xoa thanh cong, 0: Xoa khong thanh cong

//4.Xoa phan tu dau 
int deleteNodefirst(Node* &head)
{
	if(head==NULL)
		return 0;
	Node* temp = head;
	head=head->pNext;
	delete temp;
	return 1;
}

//5.Xoa phan tu x dau tien co trong danh sach
int deleteNodefirstx(Node* &head, int x)
{
	
	if(head==NULL)
		return 0;
	if(head->data==x)
		return deleteNodefirst(head);
	else
	{
		Node* cur=head;
		while (cur->pNext!=NULL && cur->pNext->data!=x)
			cur = cur->pNext;
		if(cur->pNext!=NULL)
		{
			Node* temp = cur->pNext;
			cur->pNext = temp->pNext;
			delete temp;
			return 1;
		}
	}
}

//6.Xoa tat ca phan tu x co trong danh sach
int deleteNodeAllx(Node* &head, int x)
{
	if(head==NULL)
		return 0;
	Node* temp = new Node;
	while(head->data==x) 
	{
		temp=head;
		head=head->pNext;
		delete temp;
	}
	Node* cur = head;
	while(cur->pNext!=NULL)
	{
		if (cur->pNext->data==x)
		{
			temp=cur->pNext;
			cur->pNext=temp->pNext;
			delete temp;
		}
		else
			cur=cur->pNext;
	}
	return 1;
}

//1: Liet ke co phan tu thoa yeu cau; 0: danh sach rong

//7.Liet ke tat ca so nguyen
int listint(Node* head)
{
	if(head==NULL)
		return 0;
	Node* cur = head;
	while (cur!=NULL)
	{
		if (cur->data-(int)cur->data==0)  //Lay phan nguyen
			cout<<cur->data<<" ";
		cur=cur->pNext;
	}
	cout<<endl;
	return 1;

}

//8. Liet ke phan tu k bang do chenh lech giua tong truoc va tong sau
int listk(Node* head)
{
	if(head==NULL || head->pNext==NULL)
		return 0;
	int sumfirst=0, sumend=0;
	//Xet head co phai la k khong
	Node* cur=head->pNext;
	while (cur!=NULL)
	{
		sumend=sumend+cur->data;
		cur=cur->pNext;
	}
	if (head->data==sumend)
		cout<<head->data<<" ";
	//Xet cac phan tu khac co phai la k khong
	Node* temp=head->pNext;
	cur=head;
	while (temp!=NULL)
	{
		sumfirst=sumfirst+cur->data;	//Tong cac phan tu truoc k
		sumend=sumend-temp->data;	   // Tong cac phan tu sau k
		if(temp->data==sumfirst-sumend||temp->data==sumend-sumfirst)
			cout<<temp->data<<" ";
		temp=temp->pNext;
		cur=cur->pNext;
	}
	return 1;
}

//9. Xoa tat ca
void deleteAll(Node* &head)
{
	Node* temp=head;
	while(head!=NULL)
	{
		head = head->pNext;
		delete temp;
		temp = head;
	}
}

//Hien ra cac ket qua
void display(Node* head)
{
	Node* cur = head;
	while(cur)
	{
		cout<<cur->data<<" ";
		cur=cur->pNext;
	}
	cout<<endl;
}